<template>
  <div>
    <el-form :model="form" ref="form" label-width="80px">
      <el-form-item label="题目" prop="title">
        <span>{{ form.title }}</span>
      </el-form-item>
      <el-form-item label="附件" prop="file" v-if="form.fileId">
        <span>{{ getDownLink(form) }}</span>
      </el-form-item>
      <el-form-item label="单选" prop="answers" v-if="form.type == '1' ">
        <el-radio-group v-model="form.answers">
          <div v-for="o in form.optionList">
            <el-radio :key="o.id+''" :label="o.id+''" >{{ o.title }}</el-radio>
          </div>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="多选" prop="answers" v-if="form.type == '2' ">
        <el-checkbox-group v-model="form.answers">
          <div v-for="o in form.optionList">
            <el-checkbox :key="o.id+''" :label="o.id+''"> {{ o.title }} </el-checkbox>
          </div>
          </el-checkbox-group>
      </el-form-item>
      <el-form-item label="判断题" prop="answers" v-if="form.type == '3' ">
        <el-radio-group v-model="form.answers">
          <el-radio
            v-for="dict in answerOptions"
            :key="dict.value"
            :label="dict.value"
          >{{ dict.title }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="填空" prop="answers" v-if="form.type == '4' ">
        <el-input v-model="form.answers" placeholder="请输入您的答案" />
      </el-form-item>
    </el-form>
    <el-alert :title="'答案解析: '+form.analyzes" type="info" v-if="form.check != null"></el-alert>
  </div>
</template>

<script>

  export default {
    name: "Question",
    props: {
      form: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        // 科目树选项
        answerOptions: [
          {value:'Y', title: '正确'},
          {value:'N', title: '错误'},
        ]
      };
    },
    methods: {
      check(){
        if(this.form.type == '1'){ //单选
          for(var i=0;i<this.form.optionList.length;i++){
            var option = this.form.optionList[i];
            if(option.isAnswer == 'Y' && this.form.answers != option.id){
              this.form.check = false;
            }
          }
        }else if(this.form.type == '2'){ //多选
          var uas = this.form.answers.sort();
          var aa = new Array();
          for(var i=0;i<this.form.optionList.length;i++){
            var option = this.form.optionList[i];
            if(option.isAnswer == 'Y'){
              aa.push(option.id)
            }
          }
          aa.sort();
          this.form.check = uas.join('') == aa.join('');
        }else{
          this.form.check = this.form.answers.trim() == this.form.answer.trim();
        }
      },
      /** 下载附件链接 */
      getDownLink(row) {
        if(row.file && row.file.id){
          return '<a href="'+process.env.VUE_APP_BASE_API+'/'+row.file.path+'" target="_blank">'+row.file.title+'</a>';
        }
      }
    }
  };
</script>

<style>
</style>
